package rf.db;

import java.io.File;
import java.io.IOException;
import java.sql.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class DBconnection {
		
	public Connection getConnection(){
		
		Connection conn=null;
		
		/*
		String driverName ="com.microsoft.sqlserver.jdbc.SQLServerDriver";
		String url = "jdbc:sqlserver://localhost:1433;Databasename=db_library";
		String userName="scott";
		String passWord="123456";
		*/
		
		/*String driverName ="oracle.jdbc.driver.OracleDriver";
		String url = "jdbc:oracle:thin:@localhost:1521:orcl";
		String userName="scott";
		String passWord="123456";
		*/
		
		String driverName ="";
		String url = "";
		String userName="";
		String passWord="";
		
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		
		
		DocumentBuilder db = null;
		try {
			   db = dbf.newDocumentBuilder();
			   Document doc = db.parse("DB.xml");
			  //�õ���ڵ�
			   Element root = doc.getDocumentElement();
			   //�õ�����driverName
			   NodeList tDriverName = root.getElementsByTagName("driverName");
			   //�õ���һ��driverName�ڵ�
			   Element eDriverName = (Element) tDriverName.item(0);
			   
			 //�õ�����url
			   NodeList tUrl = root.getElementsByTagName("url");
			   //�õ���һ��driverName�ڵ�
			   Element eUrl = (Element) tUrl.item(0);
			   
			 //�õ�����dbName
			   NodeList tDbName = root.getElementsByTagName("dbName");
			   //�õ���һ��driverName�ڵ�
			   Element eDbName = (Element) tDbName.item(0);
			   
			 //�õ�����userName
			   NodeList tUserName = root.getElementsByTagName("userName");
			   //�õ���һ��driverName�ڵ�
			   Element eUserName = (Element) tUserName.item(0);
			   
			 //�õ�����passWord
			   NodeList tPassWord = root.getElementsByTagName("passWord");
			   //�õ���һ��driverName�ڵ�
			   Element ePassWord = (Element) tPassWord.item(0);
			   
			   //��
			   driverName = eDriverName.getTextContent();
			   //���ӵ�ַ
			   url = eUrl.getTextContent();
			   url = url+eDbName.getTextContent();
			   //��ݿ��û���
			   userName = eUserName.getTextContent();
			   //��ݿ�����
			   passWord = ePassWord.getTextContent();
			
		} catch (Exception e2) {
			e2.printStackTrace();
		}
	
		try {
			Class.forName(driverName);
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		}
		try {
			conn=DriverManager.getConnection(url,userName,passWord);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return conn;
		
	}
	
	public void closeConnection(Connection conn){
		try{
			conn.close();
		}catch(SQLException e){
			e.printStackTrace();	
		}
	}
	
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		DBconnection db = new DBconnection();
		Connection conn= db.getConnection();
		System.out.println(conn);

	}
	
		
		
}
